﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ClientManager.Data.Infrastucture;
using ClientManager.Model;
using ClientManager.Service.Clients;
using Moq;
using NUnit.Framework;

namespace ClientManager.Service.Test
{
    [TestFixture]
    public class ClientServiceTest
    {
        private ClientService _clientService;
        private Mock<IUnitOfWork> _unitOfWorkMock;
        private Mock<IRepository<Client>> _clientRepository;


        [SetUp]
        public void SetUp()
        {
            _unitOfWorkMock = new Mock<IUnitOfWork>();
            _clientRepository = new Mock<IRepository<Client>>();
            _unitOfWorkMock.Setup(x => x.GetRepository<Client>()).Returns(_clientRepository.Object);
            _clientService = new ClientService(_unitOfWorkMock.Object);
        }

        [TearDown]
        public void TearDown()
        {
            _unitOfWorkMock = null;
            _clientRepository = null;
            _clientService = null;
        }

        [Test]
        public void Update_TheClientKeepsTheDateWhenWasCreated()
        {
            //Arrange
            var dataCreated = DateTime.Now.AddDays(-1);
            var client = new Client {ClientId=1,FirstName = "John", Surname = "Erban", ServiceLevelId = 2, DateCreated = DateTime.Now};
            var clientWithChanges = new Client { ClientId = 1, FirstName = "Juan", Surname = "Erban", ServiceLevelId = 3 };
            _clientRepository.Setup(x => x.Get(It.IsAny<Expression<Func<Client, bool>>>(), It.IsAny<Expression<Func<Client, object>>[]>())).Returns(client);

            //Act
            _clientService.Update(clientWithChanges);

            //Assert
            _clientRepository.Verify(x => x.Update(It.Is<Client>(c => c.DateCreated == client.DateCreated && c.FirstName== clientWithChanges.FirstName)));
        }

        [Test]
        public void Create_CreatesSuccessfullyAClient()
        {
            //Arrange
            var dateCreated = DateTime.Now;
            var newClient = new Client { ClientId = 1, FirstName = "John", Surname = "Erban", ServiceLevelId = 2, DateCreated = dateCreated };

            //Act
            _clientService.Create(newClient);

            //Assert
            _clientRepository.Verify(x => x.Add(It.Is<Client>(c => c.DateCreated == dateCreated && c.FirstName == newClient.FirstName && c.Surname == newClient.Surname && c.ServiceLevelId == newClient.ServiceLevelId)));
        }

        [Test]
        public void Delete_DeletesSuccessfullyAClient()
        {
            //Arrange
            var dateCreated = DateTime.Now;
            var newClient = new Client { ClientId = 1, FirstName = "John", Surname = "Erban", ServiceLevelId = 2, DateCreated = dateCreated };

            //Act
            _clientService.Delete(newClient);

            //Assert
            _clientRepository.Verify(x => x.Delete(It.Is<Client>(c => c.DateCreated == dateCreated && c.FirstName == newClient.FirstName && c.Surname == newClient.Surname && c.ServiceLevelId == newClient.ServiceLevelId)));
        }
    }
}
